
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="Connect.*" import = "java.util.Vector" import = "java.util.*" %>
    

<%
	// Redirect to the home page if the user is not a manager or employee.
	if (session.getAttribute("user_type") == null) {
		response.sendRedirect("no_permission.jsp");
	} else if (!session.getAttribute("user_type").equals("2")) {
		response.sendRedirect("index.jsp");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//Dth HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dth">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>
</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>My Bid History</h1>
		<hr></hr>
		<%
			// Getting information and updating the database.
			java.sql.Connection conn = null;
			try {
				// Connect to the database.
				Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", ConnectionInfo.myUserID());
				sysprops.put("password", ConnectionInfo.myPassword());
				conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);
				
				String auction_owner = (String)session.getAttribute("auction_user");
				String username = "";
				
				if(auction_owner==null){
					username = (String)session.getAttribute("logged_user");
				}else{
					username = auction_owner;
				}
				 
				
		%>
			<h2> Current Auctions</h2>			
		<%
				// Check if the requested username and password combination exists.
				
				java.sql.Statement statement = conn.createStatement();
				java.sql.ResultSet rs = statement
						.executeQuery("SELECT DISTINCT auctions.id, item, name,owner, open_date, end_date" +
									  " FROM bids JOIN auctions JOIN items on item=items.id" +
									  " WHERE bidder_username = '" + username + 
									  "' AND auction_id = auctions.id" +
									  " AND NOW() BETWEEN open_date AND end_date");
				out.println("<table class=\"center data-table\"><tr><th>Auction Id</th>" +
									  							   "<th>Item Name</th>"+																
									  							   "<th>End Date</th>" 
									  							   +"<th>Owner</th></tr>");
			
				while (rs.next()) {
					out.println("<tr><td>" + rs.getString("auctions.id") + "</td><td>"
							+ rs.getString("items.name") + "</td><td>"
							+ rs.getString("end_date") + "</td><td>"
							+"<a href=\"customer_auction.jsp?auc="+ rs.getString("owner")
							+"\">" + rs.getString("owner") + "</a></td></tr>");
				}
				out.println("</table>");
			%>
			<h2> Auctions Won</h2>
			<%
			 	rs = statement.executeQuery("SELECT items.name,item,bid_price,auctions.id,end_date,owner" +
											" FROM bids A JOIN auctions JOIN items ON item=items.id JOIN"+
			 								" (SELECT MAX(bid_price)bp, auction_id FROM bids GROUP BY auction_id)AS B" +
			 								" WHERE bp = bid_price AND A.auction_id = auctions.id AND B.auction_id = auctions.id" +
			 								" AND bidder_username = '" + username + "'" +
			 								" AND bid_price >= reserve_price" +
											" AND NOW() >= end_date ORDER BY end_date");
				out.println("<table class=\"center data-table\"><tr><th>Auction Id</th>" +
					 										  "<th>Item Name</th>"+
												 			  "<th>Winning Bid</th>"+
														      "<th>End Date</th>" +
												 			  "<th>Owner</th></tr>");
				while (rs.next()) {
					out.println("<tr><td>" + rs.getString("id") + "</td><td>"
							+ rs.getString("items.name") + "</td><td>"
							+ rs.getString("bid_price") + "</td><td>"
							+ rs.getString("end_date") + "</td><td>" 
							+"<a href=\"customer_auction.jsp?auc="+ rs.getString("owner")
							+"\">" + rs.getString("owner") + "</a></td></tr>");
					}
				out.println("</table>");
			
			%>
			<h2> Auctions Lost</h2>
			<%
			Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
			java.util.Properties sysprops2 = System.getProperties();
			sysprops.put("user", ConnectionInfo.myUserID());
			sysprops.put("password", ConnectionInfo.myPassword());
			java.sql.Connection conn2 = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);
			
			java.sql.Statement statement2 = conn2.createStatement();
			
			rs = statement.executeQuery("SELECT DISTINCT items.name, item,auctions.id,end_date,owner" +
					" FROM bids A JOIN auctions JOIN items ON item=items.id JOIN"+
						" (SELECT MAX(bid_price)bp, auction_id FROM bids GROUP BY auction_id)AS B" +
						" WHERE bp != bid_price AND A.auction_id = auctions.id AND B.auction_id = auctions.id" +
						" AND bidder_username = '" + username + "'" + 
					" AND NOW() >= end_date ORDER BY end_date");
			
			java.sql.ResultSet rs2 = statement2.executeQuery("SELECT items.name, item,bid_price,auctions.id,end_date,owner" +
					" FROM bids A JOIN auctions JOIN items ON item=items.id JOIN"+
						" (SELECT MAX(bid_price)bp, auction_id FROM bids GROUP BY auction_id)AS B" +
						" WHERE bp = bid_price AND A.auction_id = auctions.id AND B.auction_id = auctions.id" +
						" AND bidder_username = '" + username + "'" +
						" AND bid_price >= reserve_price" +
					" AND NOW() >= end_date ORDER BY end_date");
			
			
				out.println("<table class=\"center data-table\"><tr><th>Auction Id</th>" +
										  "<th>Item Name</th>"+
								     	   "<th>End Date</th>"
								     	  +"<th>Owner</th></tr>");
				
				Vector<String> wonauct = new Vector<String>();
				while(rs2.next()){
					wonauct.add(rs2.getString("id"));
				}
				
				 boolean won;
				 
				while (rs.next()){
					won = false;
					Iterator<String> wit = wonauct.iterator();
					String s;
					while(wit.hasNext()){
						s = wit.next();
						if(rs.getString("id").equals(s)){
							won = true;
						}
					}
					
					 if(won == false){
						 out.println("<tr><td>" + rs.getString("id") + "</td><td>"
									+ rs.getString("items.name") + "</td><td>"
									+ rs.getString("end_date") + "</td><td>"
									+"<a href=\"customer_auction.jsp?auc="+ rs.getString("owner")
									+"\">" + rs.getString("owner") + "</a></td></tr>");
					 }
					
				}
				out.println("</table>");
			/*	
			while (rs.next()) {
					out.println("<tr><td>" + rs.getString("id") + "</td><td>"
					+ rs.getString("item") + "</td><td>"
					+ rs.getString("end_date") + "</td><td>"
					+"<a href=\"customer_auction.jsp?auc="+ rs.getString("owner")
					+"\">" + rs.getString("owner") + "</a></td></tr>");
					}
					out.println("</table>");
					*/
					
			%>
			
			<h2> Did Not Meet Reserve</h2>
			<%
			rs = statement.executeQuery("SELECT item,bid_price,id,end_date,reserve_price,owner" +
					" FROM bids A JOIN auctions JOIN"+
						" (SELECT MAX(bid_price)bp, auction_id FROM bids GROUP BY auction_id)AS B" +
						" WHERE bp = bid_price AND A.auction_id = id AND B.auction_id = id" +
						" AND bidder_username = '" + username + "'" +
						" AND bid_price < reserve_price" +
					" AND NOW() >= end_date ORDER BY end_date");
					out.println("<table class=\"center data-table\"><tr><th>Auction Id</th>" +
										  "<th>Item Name</th>"+
						 			  "<th>Winning Bid</th>"+
						 			 "<th>Reserve Price</th>"+
								      "<th>End Date</th>" 
								      +"<th>Owner</th></tr>");
				while (rs.next()) {
				out.println("<tr><td>" + rs.getString("id") + "</td><td>"
				+ rs.getString("item") + "</td><td>"
				+ rs.getString("bid_price") + "</td><td>"
				+ rs.getString("reserve_price") + "</td><td>"
				+ rs.getString("end_date") + "</td><td>"
				+"<a href=\"customer_auction.jsp?auc="+ rs.getString("owner")
				+"\">" + rs.getString("owner") + "</a></td></tr>");
				}
out.println("</table>");
			
				
				
			} catch (Exception e) {
				e.printStackTrace();
				out.print(e.toString());
			} finally {
				try {
					session.setAttribute("auction_user", null);
					conn.close();
				} catch (Exception ee) {
				}
			}
		%>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html>